package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.inter.InterAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.GoodsDetailResponseBean;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;

import org.xutils.common.Callback;

import java.util.List;

/**
 * Created by Administrator on 2016/8/3.
 */
public class SelectorAdapter extends InterAdapter<GoodsDetailResponseBean.DataBean.SpecBean> {

    public SelectorAdapter(Context context, List<GoodsDetailResponseBean.DataBean.SpecBean> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dialog_color, parent, false);
        }

        RelativeLayout rl_item = ViewUtils.ViewHolder.get(convertView, R.id.rl_item);
        final TextView tv_item = ViewUtils.ViewHolder.get(convertView, R.id.tv_item);
        final ImageView iv_item = ViewUtils.ViewHolder.get(convertView, R.id.iv_item);

        if (selected == position)
            rl_item.setBackgroundResource(R.drawable.shape_border_fc4e4e_ffffff);
        else rl_item.setBackgroundResource(R.drawable.shape_border_dedede_ffffff);

        final GoodsDetailResponseBean.DataBean.SpecBean specBean = datas.get(position);

        if (!ViewUtils.isEmpty(specBean.getSpec_img())) {
            XUtils.image(iv_item, specBean.getSpec_img(), new Callback.CommonCallback<Drawable>() {
                @Override
                public void onSuccess(Drawable result) {
                    iv_item.setImageDrawable(result);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    iv_item.setVisibility(View.GONE);
                    if (!ViewUtils.isEmpty(specBean.getSpec_value())) {
                        tv_item.setVisibility(View.VISIBLE);
                        tv_item.setText(specBean.getSpec_value());
                    }
                }

                @Override
                public void onCancelled(CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });
        } else if (!ViewUtils.isEmpty(specBean.getSpec_value())) {
            tv_item.setVisibility(View.VISIBLE);
            tv_item.setText(specBean.getSpec_value().length() <= 10 ?
                    specBean.getSpec_value() : specBean.getSpec_value().substring(0, 10));
        }

//        ViewGroup.LayoutParams layoutParams = rl_item.getLayoutParams();
//        if(tv_item.getVisibility()==View.GONE){
//            layoutParams.width=110*context.getResources().getDisplayMetrics().widthPixels/1080;
//            layoutParams.height=110*context.getResources().getDisplayMetrics().widthPixels/1080;
//        }else {
//
//        }
//        rl_item.setLayoutParams(layoutParams);

        return convertView;
    }

    int selected = -1;

    public void setSelected(Integer selected) {
        this.selected = selected;
        notifyDataSetChanged();
    }
}
